extends Node2D

var pos
var layer:TileMapLayer
var target:int
var level
var prepared=false
var light_house
var heart

@onready var sound: AudioStreamPlayer2D = $sound

func _ready() -> void:
	heart=get_tree().get_first_node_in_group("heart")

func _setting(set_pos,set_layer):
	pos=set_pos
	layer=set_layer
	self.global_position=layer.map_to_local(pos)
	get_node("aim").text=str(global.flag_list[level[0]])+" / "+str(target)
	prepared=true

func _physics_process(delta: float) -> void:
	if prepared:
		if global.flag_list[level[0]]==target and not global.flag_list[level[1]]:
			if pos==heart.pos:
				global.flag_list[level[1]]=1
				layer.set_cell(light_house,3,Vector2i(1,0))
				sound.play()
				global.cur_checkpoint+=1
			else:
				get_node("aim").text=str(global.flag_list[level[0]])+" / "+str(target)
		elif global.flag_list[level[1]]:
			get_node("aim").text="SUCCESS!"
		else:
			get_node("aim").text=str(global.flag_list[level[0]])+" / "+str(target)
			
